package com.zstu.mall.web.user;

import com.zstu.mall.entity.*;
import com.zstu.mall.entity.pojo.ResultBean;
import com.zstu.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")
class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductClassService productClassService;

    @Autowired
    private ShopCartService shopCartService;

    @Autowired
    private ProductActivityService productActivityService;

    @Autowired
    private ActivityColumnService activityColumnService;
    /*@Autowired
    private CommentService commentService;*/
    @Autowired
    private UserService userService;

    /*@RequestMapping("/comment.do")
    public void comment(
                         int id,
                         String name,
                         String time,
                         int likes,
                         String content,
                         String image,
                         int commentnum,
                         HttpServletResponse response) throws IOException {
//        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String datetime = tempDate.format(new Date());
        int n=(int)(Math.random()*100)+1;
        Integer A=Integer.valueOf(n);
        Comment comment = new Comment();
        comment.setId(id);
        comment.setName("123");
        comment.setTime("123");
        comment.setLikes(likes);
        comment.setContent(content);
        comment.setImage("/mall/admin/product/img/10.jpg");
       comment.setCommentnum(commentnum);
        commentService.create(comment);
        commentService.save(comment);
        // 重定向刷新
       response.sendRedirect("/mall/product/info.html");
    }*/

    /**
    *所有产品
    */
    @RequestMapping("/")
    public String allProduct() {
        return "mall/allProducts";
    }

     /**
     * 获取商品信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/get.do")
    public ResultBean<Product> getProduct(String id) {
        Product product = productService.get(id);
        return new ResultBean<>(product);
    }

    /*@RequestMapping("/getcomment.do")
    public ResultBean<Comment> getComment(int id) {
       Comment comments = commentService.findCommentById(id);
     return new ResultBean<>(comments);
    }*/

    /**
     * 打开商品详情页面
     *
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/get.html")
    public String toProductPage(String id, Map<String, Object> map) {
        Product product = productService.get(id);
        //User user =userService.get(id);
        map.put("product", product);
        System.out.println(product);
        //map.put("user", user);
        return "mall/product/info";
    }

    /**
     * 首页初始数据
     * 3热门,4精选,5轮播图,6促销商品
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/init.do")
    public ResultBean<Map<String, List<Product>>> getInitProduct() {
        Map<String, List<Product>> stringListMap = new HashMap<>();
        //轮播图lunbo:5，0，3
        Pageable pageable = new PageRequest(0, 3, Sort.Direction.DESC, "sales");
        List<Product> products = productService.findHotProduct("5", pageable);
        products = productService.findSoldOn(products);
        stringListMap.put("lunbo", products);
        //新品new:findNewProduct，0，7(新品推荐，精心挑选)
        pageable = new PageRequest(0,7);
        products = productService.findNewProduct(pageable);
        products = productService.findSoldOn(products);
        stringListMap.put("new", products);

        int pageNum = 0;
        //判断是否为商品
        for(int i=4; i <= 7; i++){
            if(activityColumnService.get(Integer.toString(i)).getStatus().equals("1"))
                pageNum++;
        }
        //精选select:4，0，pageNum(全球大牌优选，买手用心挑选)
            pageable = new PageRequest(0, pageNum, Sort.Direction.DESC, "sales");
        products = productService.findHotProduct("4", pageable);
        products = productService.findSoldOn(products);
        stringListMap.put("select", products);

        pageNum = 0;
        //判断是否为商品
        for(int i=1; i <= 3; i++){
            if(activityColumnService.get(Integer.toString(i)).getStatus().equals("1"))
                pageNum++;
        }
        //促销promot:6，0，pageNum(专题活动，现实促销)
        pageable = new PageRequest(0, 3, Sort.Direction.DESC, "sales");
        products = productService.findHotProduct("6", pageable);
        products = productService.findSoldOn(products);
        stringListMap.put("promot", products);

        //热门hot:3，0，10(青竹良品，你的家居首选)
        pageable = new PageRequest(0, 10, Sort.Direction.DESC, "sales");
        products = productService.findHotProduct("3", pageable);
        products = productService.findSoldOn(products);
        stringListMap.put("hot", products);

        return new ResultBean<>(stringListMap);
    }

    /**
     * 查找热门、精选商品
     * 3热门,4精选,5轮播图,6促销商品
     * @param status,pageNo,pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/hot.do")
    public ResultBean<List<Product>> getHotProduct(String status, int pageNo, int pageSize) {
        Pageable pageable = new PageRequest(pageNo, pageSize);
        List<Product> products = productService.findHotProduct(status, pageable);
        products = productService.findSoldOn(products);
        return new ResultBean<>(products);
    }

    /**
     * 模糊查询
     * @param name,pageNo,pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/findLike.do")
    public ResultBean<List<Product>> getLikeProduct(String name, int pageNo, int pageSize) {
        Pageable pageable = new PageRequest(pageNo, pageSize);
        List<Product> products = productService.findByNameIsLike("%"+name+"%", pageable);
        products = productService.findSoldOn(products);
        return new ResultBean<>(products);
    }

    /**
     * 查找最新商品
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/new.do")
    public ResultBean<List<Product>> getNewProduct(int pageNo, int pageSize) {
        Pageable pageable = new PageRequest(pageNo, pageSize);
        List<Product> products = productService.findNewProduct(pageable);
        products = productService.findSoldOn(products);
        return new ResultBean<>(products);
    }

    /**
     * 所有商品
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/all.do")
    public ResultBean<List<Product>> getAllProduct() {
        List<Product> products = productService.findAll();
        products = productService.findSoldOn(products);
        return new ResultBean<>(products);
    }

    /**
     * 打开分类查看商品页面
     *
     * @return
     */
    @RequestMapping("/category.html")
    public String toCatePage(String cid, Map<String, Object> map) {
        ProductClass productClass = productClassService.get(cid);
        map.put("category", productClass);
        return "mall/product/category";
    }

    @RequestMapping("/toCart.html")
    public String toCart(){
        return "mall/product/shopCart";
    }

    /**
     * 按分类查找商品
     * @return
     */
    @ResponseBody
    @RequestMapping("/classColumn.do")
    public void getClassColumn() {

    }

    /**
     * 按分类查找商品
     *
     * @param type
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/category.do")
    public ResultBean<List<Product>> getCategoryProduct(String type, int pageNo, int pageSize) {
        Pageable pageable = new PageRequest(pageNo, pageSize);
        List<Product> products = productService.findByType(type, pageable);
        products = productService.findSoldOn(products);
        return new ResultBean<>(products);
    }

    /**
     * 按活动查找商品
     *
     * @param activityId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/activity.do")
    public ResultBean<List<Product>> getActivityProduct(String activityId, int pageNo, int pageSize) {
        Pageable pageable = new PageRequest(pageNo, pageSize);
        List<Product> products = productService.findByActivityId(activityId, pageable);
        products = productService.findSoldOn(products);
        return new ResultBean<>(products);
    }

    /**
     * 按特定顺序查找商品
     * @param type 0所有商品,1木制,2不锈钢,3陶瓷,4塑料
     * @param sort sales为销量，price为价格
     * @param dire 1为升序，0为降序
     * @return
     */
    @ResponseBody
    @RequestMapping("/sort.do")
    public ResultBean<List<Product>> getSortProduct(String type, String sort, String dire, int pageNo, int pageSize) {
        Sort var = new Sort(Sort.Direction.ASC, sort);//升序
        if(dire.equals("0"))
            var = new Sort(Sort.Direction.DESC, sort);//降序
        Pageable pageable = new PageRequest(pageNo, pageSize, var);
        List<Product> products = productService.findByOrder(var, type, pageable);
        products = productService.findSoldOn(products);
        return new ResultBean<>(products);
    }

    /**
     * 添加进入购物车
     *
     * @param productId
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/addCart.do")
    public ResultBean<Boolean> addToCart(String productId, HttpServletRequest request) throws Exception {
        shopCartService.addCart(productId, request);
        return new ResultBean<>(true);
    }

    /**
     * 移除购物车
     *
     * @param id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/delCart.do")
    public ResultBean<Boolean> delToCart(String id, HttpServletRequest request) throws Exception {
        shopCartService.remove(id, request);
        return new ResultBean<>(true);
    }

    /**
     * 查看购物车商品
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/listCart.do")
    public ResultBean<List<UserOrder>> listCart(HttpServletRequest request) throws Exception {
        List<UserOrder> orderItems = shopCartService.listCart(request);
        return new ResultBean<>(orderItems);
    }

    /**
     * 修改购物车
     *
     * @param id,number
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateCart.do")
    public ResultBean<Boolean> updateToCart(String id, Double number, HttpServletRequest request) throws Exception {
        shopCartService.updateCart(id, number, request);
        return new ResultBean<>(true);
    }

}